package com.aube.interact.fighter;

import android.animation.Animator;
import android.animation.AnimatorListenerAdapter;
import android.animation.ObjectAnimator;
import android.animation.ValueAnimator;
import android.app.Activity;
import android.content.Context;
import android.graphics.BitmapFactory;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.view.animation.AccelerateInterpolator;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.aube.interact.fighter.anim.ParticleSystem;
import com.aube.interact.fighter.roundedimageview.RoundedImageView;
import com.huyn.baseframework.utils.UserUtil;

/**
 * Created by huyaonan on 16/11/22.
 */
public class GameDrawLayout extends LinearLayout {

    private RoundedImageView mDrawBg;
    private ImageView mPrizeFragment, mPrizeBox;
    private View mPrizeGet, mPrizeOpen;
    private ImageView mPrizeTitleImg, mPrizeLogo;
    private TextView mPrizeTitle, mPrizeDesc;
    private View mDrawOpenLayout, mDrawDetailLayout;

    private View mBlank;

    private IBlurBgListener mListener;
    private Activity mActivity;

    public GameDrawLayout(Context context) {
        this(context, null);
    }

    public GameDrawLayout(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public GameDrawLayout(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);

        LayoutInflater.from(context).inflate(R.layout.aube_intera_game_draw, this, true);

        mDrawBg = (RoundedImageView) findViewById(R.id.draw_bg);
        mPrizeFragment = (ImageView) findViewById(R.id.draw_fragment);
        mPrizeBox = (ImageView) findViewById(R.id.draw_box);

        mPrizeOpen = findViewById(R.id.draw_start);
        mPrizeGet = findViewById(R.id.draw_getprize);

        mPrizeTitle = (TextView) findViewById(R.id.draw_prize_title);
        mPrizeTitleImg = (ImageView) findViewById(R.id.draw_prize_title_img);
        mPrizeLogo = (ImageView) findViewById(R.id.draw_prize_logo);
        mPrizeDesc = (TextView) findViewById(R.id.draw_prize_desc);

        mDrawOpenLayout = findViewById(R.id.draw_open_layout);
        mDrawDetailLayout = findViewById(R.id.draw_detail_layout);

        mBlank = findViewById(R.id.draw_blank);

        mPrizeOpen.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                //do exposion anim
                explodeToGetPrize();
            }
        });

        mPrizeGet.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                getPrize();
            }
        });
    }

    private void explodeToGetPrize() {
        int[] locationLeft = new int[2];
        int[] locationRight = new int[2];
        mPrizeFragment.getLocationInWindow(locationLeft);
        mPrizeBox.getLocationInWindow(locationRight);
        int distance = (locationRight[0] - locationLeft[0])/2;
        final ValueAnimator anim = ValueAnimator.ofFloat(0f, distance);
        anim.setDuration(200);
        anim.setInterpolator(new AccelerateInterpolator());
        anim.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
            @Override
            public void onAnimationUpdate(ValueAnimator animation) {
                float value = (float) animation.getAnimatedValue();
                mPrizeFragment.setTranslationX(value);
                mPrizeBox.setTranslationX(-value);
            }
        });
        anim.addListener(new AnimatorListenerAdapter() {
            @Override
            public void onAnimationEnd(Animator animation) {
                //do explosion
                explodeAnim();

//                fadeOutLayout();
                mDrawOpenLayout.setVisibility(GONE);
                showPrizeDetailLayout();
            }

            @Override
            public void onAnimationStart(Animator animation) {
                mDrawDetailLayout.setVisibility(View.INVISIBLE);
            }
        });
        anim.start();
    }

    private void fadeOutLayout() {
        ObjectAnimator anim = ObjectAnimator.ofFloat(mDrawOpenLayout, "alpha", 1f, 0f);
        anim.setDuration(200);
        anim.addListener(new AnimatorListenerAdapter() {
            @Override
            public void onAnimationEnd(Animator animation) {
                mDrawOpenLayout.setVisibility(View.GONE);
            }
        });
        anim.start();
    }

    private void showPrizeDetailLayout() {
        Animation anim = AnimationUtils.loadAnimation(getContext(), R.anim.prize_in);
        anim.setStartOffset(200);
        anim.setAnimationListener(new Animation.AnimationListener() {
            @Override
            public void onAnimationStart(Animation animation) {
                mDrawDetailLayout.setVisibility(View.VISIBLE);
            }

            @Override
            public void onAnimationEnd(Animation animation) {

            }

            @Override
            public void onAnimationRepeat(Animation animation) {

            }
        });
        mDrawDetailLayout.startAnimation(anim);
    }

    private void explodeAnim() {
        try {
            ParticleSystem ps = new ParticleSystem(mActivity, 300, BitmapFactory.decodeResource(getResources(), R.drawable.icon_star_2), 800);
            ps.setScaleRange(0.7f, 1.3f);
            ps.setSpeedRange(0.5f, 0.75f);
            ps.setRotationSpeedRange(90, 180);
            ps.setFadeOut(200, new AccelerateInterpolator());
            ps.oneShot(mBlank, 70);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public void attachActivity(Activity activity) {
        mActivity = activity;
    }

    private void getPrize() {
        if(UserUtil.isUserLogin()) {

        } else {
            //toast user to login
        }
    }

    private void reset() {
        mDrawOpenLayout.setVisibility(View.VISIBLE);
        mDrawOpenLayout.setAlpha(1f);
        mDrawDetailLayout.setVisibility(View.GONE);
        mPrizeBox.setTranslationX(0);
        mPrizeFragment.setTranslationX(0);
    }

    public void animShow() {
        reset();
        setVisibility(View.VISIBLE);
        if(mListener != null)
            mListener.showBg();
    }

    public void dismiss() {
        setVisibility(View.GONE);
        if(mListener != null)
            mListener.hideBg();
    }

    public void addBgListener(IBlurBgListener listener) {
        mListener = listener;
    }

    public interface IBlurBgListener {
        public void showBg();
        public void hideBg();
    }

}
